package com.ww.word.ui;

import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.ww.core.activity.SlideMenuActivity;
import com.ww.word.R;
import com.ww.word.adapter.HistoryAdapter;
import com.ww.word.db.StudyDbHelper;
import com.ww.word.ui.menu.LeftFragment;

/**
 * 我已学过的汉字列表界面
 * @version 2013-12-5
 * @author wangwei
 */
public class HistoryActivity extends SlideMenuActivity {
	private ListView gv;
	private List<String> studyList;
	private HistoryAdapter historyAdapter;
	private View view;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.activity_history, container, false);
		initView(view);
		return view;
	}

	private void initView(View view) {
		setTitle(view, LeftFragment.mTitleName);
		gv = (ListView) view.findViewById(R.id.listView);
		getHistory();
	}

	private void getHistory() {
		showProgressDialog(context);
		new Thread(new Runnable() {
			@Override
			public void run() {
				StudyDbHelper w = new StudyDbHelper(context);
				studyList = w.getHistoryTime();
				handler.sendEmptyMessage(1);
			}
		}).start();
	}

	// 定义一个Handler，用来异步处理数据
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (studyList.size() == 0) {
				failView(view, getString(R.string.history_empty), R.drawable.list_empty_icon);
			} else {
				historyAdapter = new HistoryAdapter(context, studyList);
				gv.setAdapter(historyAdapter);
				closeFailView(view);
			}
			hideProgressDialog();
		};
	};

}
